package com.oauth.global.server.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.gwtwidgets.server.spring.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.components.client.beans.OAuthUrl;
import com.oauth.global.client.beans.AppInfoBean;
import com.oauth.global.client.beans.ApplicationClientBean;
import com.oauth.global.client.beans.BirthdayClientBean;
import com.oauth.global.client.rpc.GlobalRemoteService;
import com.oauth.global.server.service.GlobalOAuthService;
import com.oauth.global.server.utils.ApplicationUtil;
import com.oauth.service.application.registry.beans.ApplicationInfoBean;
import com.oauth.service.generic.exception.OAuthServiceException;
import com.oauth.service.global.beans.GlobalDataType;

public class GlobalGwtController extends GWTSpringController implements GlobalRemoteService {

	private static final long serialVersionUID = -5758464341967618224L;

	@Autowired
	private GlobalOAuthService mGlobalOAuthService;

	/**
	 * @param pGlobalOAuthService
	 *            the globalOAuthService to set
	 */
	public void setGlobalOAuthService(GlobalOAuthService pGlobalOAuthService) {
		mGlobalOAuthService = pGlobalOAuthService;
	}

	public List<ApplicationClientBean> getMyApplications() {
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());

		List<ApplicationInfoBean> applicationInfo = new ArrayList<ApplicationInfoBean>();
		try {
			applicationInfo = mGlobalOAuthService.getSubscribedApplication(memberId);
		} catch (OAuthServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ApplicationUtil.convertToClientBeans(applicationInfo);
	}

	public String unSubscribeFromApplication(Long pApplicationId) {
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		try {
			mGlobalOAuthService.unSubscribeApplication(memberId, pApplicationId);
		} catch (OAuthServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}

	public List<ApplicationClientBean> getUnSubscribedApplications() {
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());

		List<ApplicationInfoBean> applicationInfo = new ArrayList<ApplicationInfoBean>();
		try {
			applicationInfo = mGlobalOAuthService.getUnsubscribedApplication(memberId);
		} catch (OAuthServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return ApplicationUtil.convertToClientBeans(applicationInfo);
	}

	@Override
	public boolean updateStatus(String pStatusText, Set<Long> pApplicationIdSet) {
		Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
		mGlobalOAuthService.updateStatus(memberId, pStatusText, pApplicationIdSet);
		return true;
	}

	@Override
	public List<AppInfoBean> getApplicationImagesForStatus() {
		Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
		try {
			return mGlobalOAuthService.getApplicationImages(memberId, GlobalDataType.STATUS_UPDATE);
		} catch (OAuthServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<AppInfoBean> getApplicationImagesForBirthday() {
		Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
		try {
			return mGlobalOAuthService.getApplicationImages(memberId,
					GlobalDataType.BIRTHDAY_NOTIFACTION);
		} catch (OAuthServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<BirthdayClientBean> getUpcomingBirthdays() {
		Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
		try {
			return mGlobalOAuthService.getUpcomingBirthdays(memberId);
		} catch (OAuthServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<BirthdayClientBean> getUpcomingBirthdays(long pAppId) {
		Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
		return mGlobalOAuthService.getUpcomingBirthdays(memberId, pAppId);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.oauth.global.client.rpc.GlobalRemoteService#getSignInUrl()
	 */
	@Override
	public OAuthUrl getSignInUrl(long applicationId) {
		return mGlobalOAuthService.getSignInUrl(applicationId);
	}

}
